package com.android.tea.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.tea.R;
import com.android.tea.TeaFriendSessionActivity;
import com.android.tea.entity.TeaMember;
import com.android.tea.entity.TeaMember.MemberType;
import com.android.tea.image.ImageLoaderManager;
import com.android.tea.util.StringUtils;
import com.android.tea.util.TeaLog;
import com.android.tea.util.Utils;

public class AllFriendsAdapter extends BaseAdapter {

	private static final String TAG = "Adapter";
	private LayoutInflater inflater;
	private List<TeaMember> list;
	private Context context;
	private ImageLoaderManager imageLoaderManager;

	public AllFriendsAdapter(Context context, List<TeaMember> list) {
		super();
		this.context = context;
		this.list = list;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoaderManager = new ImageLoaderManager(context, new Handler(),
				this);

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public TeaMember getItem(int position) {
		if (getCount() >= position + 1) {
			return list.get(position);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		TeaMember item = getItem(position);
		if (item != null) {
			return item.getId();
		} else {
			return 0;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final TeaMember teaMember = list.get(position);
		if (teaMember == null) {
			return null;
		}
		final ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.footprint_item, null);
			holder = new ViewHolder();
			holder.item = (RelativeLayout) convertView
					.findViewById(R.id.relativeLayout1);
			holder.ivAvatar = (ImageView) convertView
					.findViewById(R.id.footPrintavatar);
			holder.tvTitle = (TextView) convertView
					.findViewById(R.id.footPrint_title);
			holder.tvTime = (TextView) convertView
					.findViewById(R.id.footPrintTimestamp);
			holder.id = position;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		OnClickListener clickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.relativeLayout1:
					gotoNextActivity(teaMember.getType());
					break;
				case R.id.avatar:

					break;
				default:
					break;
				}
			}

		};

		holder.item.setOnClickListener(clickListener);
		holder.ivAvatar.setOnClickListener(clickListener);

		setData(teaMember, holder);
		return convertView;
	}

	private void setData(TeaMember teaMember, ViewHolder holder) {
		MemberType type = teaMember.getType();
		if (MemberType.TeaCommon == type) {
			String title = teaMember.getTitle();
			setImage(holder.ivAvatar, teaMember.getAvatar());
			setTitle(holder.tvTitle, title);
			setTime(holder.tvTime, teaMember != null ? teaMember.getTimeStamp()
					: 0);
		} else {
			if (MemberType.TeaTeam == type) {
				setImage(holder.ivAvatar, R.drawable.tea_team);
				setTitle(holder.tvTitle,
						context.getResources().getString(R.string.tea_team));
				setTime(holder.tvTime,
						teaMember != null ? teaMember.getTimeStamp() : 0);
			} else if (MemberType.TeaNews == type) {
				setImage(holder.ivAvatar, R.drawable.tea_news);
				setTitle(holder.tvTitle,
						context.getResources().getString(R.string.tea_news));
				setTime(holder.tvTime,
						teaMember != null ? teaMember.getTimeStamp() : 0);
			} else if (MemberType.TeaBooks == type) {
				setImage(holder.ivAvatar, R.drawable.tea_books);
				setTitle(holder.tvTitle,
						context.getResources().getString(R.string.tea_books));
				setTime(holder.tvTime,
						teaMember != null ? teaMember.getTimeStamp() : 0);
			} else if (MemberType.TeaSupplyDemand == type) {
				setImage(holder.ivAvatar, R.drawable.tea_cooperation);
				setTitle(
						holder.tvTitle,
						context.getResources().getString(
								R.string.tea_supply_demand_news));
				setTime(holder.tvTime,
						teaMember != null ? teaMember.getTimeStamp() : 0);
			} else if (MemberType.TeaShop == type) {
				setImage(holder.ivAvatar, R.drawable.tea_merchant);
				setTitle(holder.tvTitle,
						context.getResources().getString(R.string.tea_shops));
				setTime(holder.tvTime,
						teaMember != null ? teaMember.getTimeStamp() : 0);
			}
		}
	}

	private void setTime(TextView tvTime, long timeStamp) {
		if (timeStamp > 0) {
			tvTime.setText(StringUtils.getFormatTimeBeforeNow(timeStamp));
		} else {
			tvTime.setText(context.getResources().getString(R.string.unknown));
		}
	}

	private void setImage(ImageView ivAvatar, String avatar) {
		if (StringUtils.isNotNullOrEmpty(avatar)) {
			ivAvatar.setImageBitmap(imageLoaderManager.getImage(avatar,
					R.drawable.tea_default, Utils.dipToPixels(context, 74)));
		} else {
			ivAvatar.setImageResource(R.drawable.tea_default);
		}
	}

	private void setImage(ImageView ivAvatar, int resId) {
		ivAvatar.setImageResource(resId);
	}

	private void setTitle(TextView tvTitle, String title) {
		if (StringUtils.isNotNullOrEmpty(title)) {
			tvTitle.setText(title);
		} else {
			tvTitle.setText(context.getResources().getString(R.string.unknown));
		}
	}

	static class ViewHolder {
		int id;
		RelativeLayout item;
		ImageView ivAvatar;
		TextView tvTitle;
		TextView tvTime;
	}

	private void gotoNextActivity(MemberType memberType) {
		TeaLog.i(TAG, "..." + memberType);
		Intent intent = new Intent(context,TeaFriendSessionActivity.class);
		context.startActivity(intent);
	}
}
